package com.hanyc.spring.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanyc.spring.result.Result;

import com.hanyc.spring.dto.Demo2DTO;
import com.hanyc.spring.vo.Demo2VO;
import com.hanyc.spring.entity.Demo2;
import com.hanyc.spring.util.QueryUtils;
import com.hanyc.spring.service.IDemo2Service;
import com.hanyc.spring.util.MapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;



import java.util.Arrays;
import java.util.List;


/**
 * 【DEMO_2】控制器层
 *
 * @author hanyc
 * @date 2025-06-13
 */
@Slf4j
@Api(tags = {"【DEMO_2】模块Controller"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/demo2")
public class Demo2Controller {

    private final IDemo2Service  demo2Service;

    @ApiOperation(value = "DEMO_2列表查询")
    @PostMapping("/list")
    public Result<List<Demo2VO>> list(@RequestBody Demo2DTO dto) {
        return Result.ok(MapperUtils.INSTANCE.mapAsList(Demo2VO.class,demo2Service.list(Wrappers.lambdaQuery(BeanUtil.copyProperties(dto, Demo2.class)))));
    }

    @ApiOperation(value = "DEMO_2分页查询")
    @PostMapping("/page")
    public Result<Page<Demo2VO>> page(@RequestBody Demo2DTO dto) {
        Page<Demo2> page = new Page<>(dto.getCurrent(), dto.getSize());
        Page<Demo2> result = demo2Service.page(page, QueryUtils.dynamicSort(dto.getSortBy(), Wrappers.query(BeanUtil.copyProperties(dto, Demo2.class))));
        return Result.ok(MapperUtils.INSTANCE.mapAsPage(Demo2VO.class, result));
    }

    @ApiOperation(value = "DEMO_2添加,并获取ID")
    @PostMapping("/insertReturnId")
    public Result<Long> insertReturnId(@RequestBody Demo2DTO dto) {
        Demo2 entity = BeanUtil.copyProperties(dto, Demo2.class);
        demo2Service.save(entity);
        return Result.ok(entity.getId());
    }

    @ApiOperation(value = "DEMO_2添加")
    @PostMapping
    public Result<Boolean> insert(@RequestBody Demo2DTO dto) {
        Demo2 entity = BeanUtil.copyProperties(dto, Demo2.class);
        return Result.ok(demo2Service.save(entity));
    }

    @ApiOperation(value = "DEMO_2更新")
    @PutMapping
    public Result<Boolean> update(@RequestBody Demo2DTO dto) {
        Demo2 entity = BeanUtil.copyProperties(dto, Demo2.class);
        return Result.ok(demo2Service.updateById(entity));
    }

    @ApiOperation(value = "DEMO_2删除")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.ok(demo2Service.removeById(id));
    }

    @ApiOperation(value = "DEMO_2详情")
    @GetMapping("/{id}")
    public Result<Demo2VO> detail(@PathVariable("id") Long id) {
        return Result.ok(BeanUtil.copyProperties(demo2Service.getById(id), Demo2VO.class));
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids){
        return Result.ok(demo2Service.removeByIds(ids));
    }


}

